package air2.totc.messaging;

import java.util.ArrayList;
import java.util.List;

import air2.totc.common.GestionBD;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class GestionSMS {
	// Database fields
	private SQLiteDatabase database;
	private GestionBD dbHelper;
	private String[] allColumns = { GestionBD.COLUMN_ID_SMS,GestionBD.COLUMN_ID_CONTACT,GestionBD.COLUMN_SMS,GestionBD.COLUMN_CRYPTO,GestionBD.COLUMN_ETAT};
	public GestionSMS(Context context) {
		dbHelper = new GestionBD(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}
	public void erase() throws SQLException {
		database = dbHelper.getWritableDatabase();
		dbHelper.onUpgrade(database, 1, 1);
		dbHelper.close();
	}
	public void close() {
		dbHelper.close();
	}

	public Message insertSMS(String smsMessage,long Contact_id,int type_crypto,int etat) {
		ContentValues values = new ContentValues();
		values.put(GestionBD.COLUMN_SMS, smsMessage);
		values.put(GestionBD.COLUMN_ID_CONTACT, Contact_id);
		values.put(GestionBD.COLUMN_CRYPTO,type_crypto);
		values.put(GestionBD.COLUMN_ETAT,etat);
		long insertId = database.insert(GestionBD.TABLE_TOTCSMS, null,values);
		// To show how to query
		Cursor cursor = database.query(GestionBD.TABLE_TOTCSMS,
				allColumns, GestionBD.COLUMN_ID_SMS + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		return cursorToSMS(cursor);
	}

	public void deleteSMS(Message smsObject) {
		long id = smsObject.getId_sms();
		System.out.println("Comment deleted with id: " + id);
		database.delete(GestionBD.TABLE_TOTCSMS, GestionBD.COLUMN_ID_SMS
				+ " = " + id, null);
	}
	public int selectTypeCryptoFromID(int idSMS){
		//bouts de la requete
		String[] select = {GestionBD.COLUMN_CRYPTO};
		String where = GestionBD.COLUMN_ID_SMS +" = ?";
		String[] equal =  {""+idSMS};
		//resultat
		String result="";
		//requete formulée
		Cursor cursor = database.query(GestionBD.TABLE_TOTCSMS,
				select, where, equal, null, null, null);
		cursor.moveToFirst();
		int i=0;
		// parcour du cursor pour requête select
		while (!cursor.isAfterLast()) {
			result=cursor.getString(0);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return Integer.parseInt(result);
		
	}
	public String selectMessageFromID(int idSMS){
		//bouts de la requete
		String[] select = {GestionBD.COLUMN_SMS};
		String where = GestionBD.COLUMN_ID_SMS +" = ?";
		String[] equal =  {""+idSMS};
		//resultat
		String result="";
		//requete formulée
		Cursor cursor = database.query(GestionBD.TABLE_TOTCSMS,
				select, where, equal, null, null, null);
		cursor.moveToFirst();
		int i=0;
		// parcour du cursor pour requête select
		while (!cursor.isAfterLast()) {
			result=cursor.getString(0);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return result;
		
	}
	public List<Message> getAllSMS() {
		List<Message> listSMSOBJECT = new ArrayList<Message>();
		Cursor cursor = database.query(GestionBD.TABLE_TOTCSMS,
				allColumns, null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Message smsObject = cursorToSMS(cursor);
			listSMSOBJECT.add(smsObject);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return listSMSOBJECT;
	}

	private Message cursorToSMS(Cursor cursor) {
		Message smsObject = new Message();
		smsObject.setId_sms(cursor.getLong(0));
		smsObject.setId_contact(cursor.getLong(1));
		smsObject.setSms(cursor.getString(2));
		smsObject.setCrypto_type(cursor.getInt(3));
	    smsObject.setEtat(cursor.getInt(4));
		return smsObject;
	}
}
